#include "dbcFactory.h"
#include "DatabaseConnManage.h"

#ifdef DBMS_USE_SQLITE
	#include "Sqlite.h"
#endif

#ifdef DBMS_USE_MYSQL
	#include "MySql.h"
#endif

#ifdef DBMS_USE_ODBC
	#include "Odbc.h"
#endif

#ifdef DBMS_USE_SQLSERVER
	#include "SqlServer.h"
#endif

#ifdef DBMS_USE_ORACLE
	#include "Oracle.h"
#endif


LIBDBC_BEGIN_NAMESPACE

const char* dbcFactory::szDbmsNames[] = { "SqLite", "MySQL", "ODBC", "SqlServer", "Oracle", "PostgreSQL" };


dbcFactory::dbcFactory()
{
}


dbcFactory::~dbcFactory()
{
}


IDatabase* dbcFactory::createDatabaseInterface(DatabaseManageSystemT dbms)
{
	IDatabase* pDB = NULL;
	switch (dbms)
	{
#ifdef DBMS_USE_SQLITE
	case DBMS_SqLite: 
		// sqlite is always on
		//pDB = new SqLite();
		break;
#endif

#ifdef DBMS_USE_MYSQL
	case DBMS_MySQL: 
		pDB = new MySql(); 
		break;
#endif

#ifdef DBMS_USE_ODBC
	case DBMS_ODBC: 
		pDB = new Odbc(); 
		break;
#endif

#ifdef DBMS_USE_SQLSERVER
	case DBMS_SQLSERVER: 
		pDB = new SqlServer(); 
		break;
#endif

#ifdef DBMS_USE_ORACLE
	case DBMS_ORACLE: 
		pDB = new Oracle(); 
		break;
#endif
	
	default:
		break;

	}
	return pDB;
}

IDatabase* dbcFactory::createDatabaseInterface(const char* dbmsname)
{
	IDatabase* pDB = NULL;
	for (int i = 0; i < dbcFactory::dbmsCount; i++)
	{
		if (strcmp(dbcFactory::szDbmsNames[i], dbmsname) == 0)
			return dbcFactory::createDatabaseInterface(static_cast<DatabaseManageSystemT>(i));
	}

	return NULL;
}

void dbcFactory::freeDatabaseInterface(IDatabase** pdbo)
{
	if (pdbo && *pdbo)
	{
		delete *pdbo;
		*pdbo = NULL;
	}
}

const char* dbcFactory::version()
{
	return (const char*)"1.0.0";
}

const char** dbcFactory::getListOfDBMS()
{
	return (const char**)&szDbmsNames[0];
}

unsigned int dbcFactory::getCountOfDBMS()
{
	return dbmsCount;
}

bool dbcFactory::createDatabaseConnectionManager(IDatabaseConnManage** ppmgr)
{
	bool bCreated = false;
	if (ppmgr)
	{
		*ppmgr = new DatabaseConnManage();
		bCreated = *ppmgr != NULL;
	}
	return bCreated;
}

bool dbcFactory::freeDatabaseConnectionManager(IDatabaseConnManage** ppmgr)
{
	bool bDeleted = false;
	if (ppmgr && *ppmgr)
	{
		delete *ppmgr;
		*ppmgr = NULL;
		bDeleted = true;
	}
	return bDeleted;
}

LIBDBC_END_NAMESPACE